//栈_删除字符中的所有相邻重复项
//https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/description/
public class Test {
    public static void main(String[] args) {
        //
    }
}

//方法一：我写的，用到String的substring方法
class Solution1 {
    public String removeDuplicates(String s) {
        //
        String stack_S = "";
        char[] arr = s.toCharArray();
        for(int i = 0 ; i < arr.length ; i++){
            if(stack_S.length() == 0 || arr[i] != stack_S.charAt(stack_S.length()-1)){
                stack_S += arr[i];
            }else{
                stack_S = stack_S.substring(0, stack_S.length()-1);
            }
        }
        return stack_S;
    }
}

//方法二：练习一遍老师的：StringBuffer，方便添加和删除元素
class Solution {
    public String removeDuplicates(String s) {
        //
        StringBuffer res = new StringBuffer();
        char[] arr = s.toCharArray();
        for(char ch : arr){
            if(res.length() != 0 && ch == res.charAt(res.length()-1)){
                res.deleteCharAt(res.length()-1);
            }else{
                res.append(ch);
            }
        }
        return res.toString();
    }
}